package com.erp.erp_project.controller;

import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.model.Customer;
import com.erp.erp_project.model.Order;
import com.erp.erp_project.model.Result;
import com.erp.erp_project.service.CustomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;

/**
 * @author the bright star
 */
@Controller
@AllArgsConstructor
@RequestMapping("/customer")
public class CustomController {

    private CustomService customService;

    @RequestMapping("/tolist")
    public String toList(Model model){
        return "customer/list";
    }

    @RequestMapping("/toadd")
    @Permission("客户添加")
    public String toAdd(Model model){
        return "customer/add";
    }

    @RequestMapping("/toedit")
    @Permission("客户修改")
    public String toEdit(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "customer/edit";
    }
    @RequestMapping("/tonote")
    public String toNote(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "customer/requirement";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Result<PageInfo<Customer>> getList(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Customer> list=customService.getAll();
        PageInfo<Customer> pageInfo=new PageInfo<>(list);
        Result<PageInfo<Customer>> result=new Result<>(0,"查询失败");
        result.setData(pageInfo);
        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    @Permission("客户添加")
    public Result<String> addCustomer(Customer customer){
        customService.addCustomer(customer);
        return new Result<>(200,"添加成功");
    }

    @RequestMapping("/edit")
    @ResponseBody
    @Permission("客户添加")
    public Result<String> editCustomer(Customer customer){
        customService.editCustomer(customer);
        return new Result<>(200,"编辑成功");
    }

    @RequestMapping("/loadedit")
    @ResponseBody
    @Permission("客户添加")
    public Result<Customer> getCustomer(@Param("id") Integer id){
        Customer customer=customService.getCustomerById(id);
        Result<Customer> result=new Result<>(200,"得到数据");
        result.setData(customer);
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Permission("客户删除")
    public Result<String> deleteCustomer(String ids){
        String[] str=ids.split(",");
        List<Integer> list=new ArrayList<>();
        for (String id: str) {
            list.add(Integer.parseInt(id));
        }
        customService.deleteCustomer(list);
        return new Result<>(200,"删除成功");
    }

    @RequestMapping("/search")
    @ResponseBody
    public Result<PageInfo<Customer>> search(Integer page, Integer limit, Integer type, String condition){
        PageHelper.startPage(page,limit);
        List<Customer> customers=customService.search(type,condition);
        PageInfo<Customer> info=new PageInfo<>(customers);
        Result<PageInfo<Customer>> result=new Result<>(0,"查询成功");
        result.setData(info);
        return result;
    }
    @RequestMapping("/requirement")
    @ResponseBody
    public Result<String> setNote(@Param("note") String note,Integer id){
        customService.setNote(note,id);
        return new Result<>(200,"操作成功");
    }
}
